package com.lzb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzb.reggie.common.R;
import com.lzb.reggie.dto.DishDto;
import com.lzb.reggie.dto.SetMealDto;
import com.lzb.reggie.entity.Category;
import com.lzb.reggie.entity.Dish;
import com.lzb.reggie.entity.DishFlavor;
import com.lzb.reggie.entity.SetMeal;
import com.lzb.reggie.service.CategoryService;
import com.lzb.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setMealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public R<String> save(@RequestBody SetMealDto setMealDto) {
        setMealService.saveSetMealDish(setMealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = "setMealCache", key = "#p0 + '_' + #p1 + '_' + #p2")
    public R<Page<SetMealDto>> page(int page, int pageSize, String name) {

        Page<SetMeal> setMealPage = new Page<>(page, pageSize);
        Page<SetMealDto> setMealDtoPage = new Page<>();

        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, SetMeal::getName, name);
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        setMealService.page(setMealPage, queryWrapper);

        BeanUtils.copyProperties(setMealPage, setMealDtoPage, "records");

        List<SetMealDto> setMealDtoList = setMealPage.getRecords().stream().map((item) -> {

            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(item, setMealDto);

            Category category = categoryService.getById(item.getCategoryId());
            String categoryName = category.getName();
            setMealDto.setCategoryName(categoryName);
            return setMealDto;
        }).collect(Collectors.toList());

        setMealDtoPage.setRecords(setMealDtoList);

        return R.success(setMealDtoPage);
    }

    /**
     * 根据ids删除套餐以及套餐和菜品的关联关系
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        setMealService.deleteSetMealDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetMealDto> getById(@PathVariable Long id) {
        SetMealDto setMealDto = setMealService.getByIdWithDish(id);
        return R.success(setMealDto);
    }

    /**
     * 修改套餐以及套餐和菜品的关联关系
     * @param setMealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setMealCache", allEntries = true)
    public R<String> update(@RequestBody SetMealDto setMealDto) {
        setMealService.updateSetMealDish(setMealDto);
        return R.success("修改成功");
    }

    /**
     * 修改套餐的售卖状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setMealCache", allEntries = true)
    public R<String> updateStatus(@RequestParam List<Long> ids, @PathVariable Integer status) {

        List<SetMeal> setMealList = setMealService.getByIds(ids);

        setMealList = setMealList.stream().map((item) -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());

        setMealService.updateBatchById(setMealList);

        return R.success("修改成功");
    }

    /**
     * 根据套餐分类id查询套餐数据
     * @param setMeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setMealCache", key = "#setMeal.categoryId")
    public R<List<SetMeal>> list(SetMeal setMeal) {
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setMeal.getCategoryId() != null, SetMeal::getCategoryId, setMeal.getCategoryId());
        queryWrapper.eq(SetMeal::getStatus, 1);
        List<SetMeal> list = setMealService.list(queryWrapper);

        return R.success(list);
    }
}
